package com.example.demo.utils;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Description
 * @Author chenhuiyu
 * @Date 2023/12/15
 */
public class ReadExcelCompress {

    public static void readCompress(File file) throws IOException, ArchiveException {
        ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory();
        String extension = file.getPath().substring(file.getPath().indexOf(".") + 1);

        InputStream inputStream = new FileInputStream(file);
        System.out.println(file.getName());
        System.err.println("filename:" + file.getPath());
        System.out.println(extension);
        System.out.println("--------------------");

        ArchiveInputStream archiveInputStream = archiveStreamFactory.createArchiveInputStream(extension, inputStream);
        ArchiveEntry archiveEntry;
        while ((archiveEntry = archiveInputStream.getNextEntry()) != null) {
            System.out.println("archiveEntry:" + archiveEntry);
            if (!archiveEntry.isDirectory()) {
                long size = archiveEntry.getSize();
                System.out.println(archiveEntry.getName() + ":" + size);
                if (size > 0) {
                    ReadExcelUtil.readExcel(archiveEntry.getName(), archiveInputStream, 0);
                }
                System.out.println("--------------------");
            }
        }
        System.out.println("********************");
        archiveInputStream.close();
        inputStream.close();
//        System.out.println(file.delete());
    }
    public static void zip(MultipartFile multipartFile) throws IOException {
//        File file = File.createTempFile("temp", ".zip");
//        System.out.println(file.getName());
//        multipartFile.transferTo(file);
        ZipArchiveEntry zipArchiveEntry;
        ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(multipartFile.getInputStream());
        while ((zipArchiveEntry = zipArchiveInputStream.getNextZipEntry()) != null) {
            long size = zipArchiveEntry.getSize();
            if (size > 0) {
                ReadExcelUtil.readExcel(zipArchiveEntry.getName(), zipArchiveInputStream, 0);
            }
        }
        zipArchiveInputStream.close();
//        System.out.println(file.delete());
    }

    public static void tar(MultipartFile multipartFile) throws IOException {

        TarArchiveEntry tarArchiveEntry;
        TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(multipartFile.getInputStream());
        while ((tarArchiveEntry = tarArchiveInputStream.getNextTarEntry()) != null) {
            if (!tarArchiveEntry.isDirectory()) {
                long size = tarArchiveEntry.getSize();
                if (size > 0) {
                    ReadExcelUtil.readExcel(tarArchiveEntry.getName(), tarArchiveInputStream, 0);
                }
            }
        }
        tarArchiveInputStream.close();
    }
    public static void read7z(String fileName) throws IOException {
        File file = File.createTempFile("temp", ".7z");
        MultipartFile multipartFile = GetMultipartFile.createMultipartFile(fileName);
        multipartFile.transferTo(file);

//        SevenZFile sevenZFile = new SevenZFile(new File(fileName));
        SevenZFile sevenZFile = new SevenZFile(file);
        System.out.println(file.getName());
        SevenZArchiveEntry sevenZArchiveEntry;
        while ((sevenZArchiveEntry = sevenZFile.getNextEntry()) != null) {
            if (!sevenZArchiveEntry.isDirectory()) {
                long size = sevenZArchiveEntry.getSize();
                System.out.println(sevenZArchiveEntry.getName() + ":" + size);
                if (size > 0) {
                    ReadExcelUtil.readExcel(sevenZArchiveEntry.getName(), sevenZFile.getInputStream(sevenZArchiveEntry), 0);
                }
            }
        }
        // 不用手动关闭，或内置关闭
        sevenZFile.close();
        System.out.println(file.delete());
    }

}
